var wineApp = wineApp || {};
wineApp.wine = (function() {
    var params = $.url().param();
    var key = params["key"] == null ? "" : params["key"];
    var pageSize = 10
    var loadWineList = function() {
        var params = $.url().param();
        var ajaxUrl = wineApp.CONFIG.API.winelist;
        wineApp.ajax.post(ajaxUrl, {
            skey: key,
            ctype: '',
            page: 1,
            pageSize: pageSize
        }).done(onSuccess);
        onPullUp();
    }
    var onSuccess = function(data) {
        console.log(data);
        var resourceURL = wineApp.tool.getResourceURL();
        wineApp.common.buildImgLayout2Col("#wine_list", data.list, function(val) {
            //if (val.headPic == "") return null;
            var obj = {};
            var width = (wineApp.tool.getScreenWidth() * 0.46).toFixed(0);
            //obj["img"] = '../images/temp/jiu.jpg';
            obj["img"] = resourceURL + '/' + val.titleimg + '!' + width + 'x' + width;
            obj["img_w"] = "100%";
            obj["imgLink"] = "wine_detail.html?wineId=" + val.id;
            obj["imgDesc"] = "<dd>" +
                "<ul>" +
                "<li><a class='entry_title' href='products_more.html'>" + val.title + "</a></li>" +
                "<li><b>￥" + val.price + "</b><span>" + val.oprice + "</span></li>" +
                "</ul>" +
                "</dd>";
            return obj;
        });
    }

    var onPullUp = function() {
        var stopAjax = false;
        $('#scroll').infinitescroll({
            dataType: 'jsonpost',
            appendCallback: false,
            nextSelector: '#page2',
            state: {
                currPage: 1
            },
            pixelsFromNavToBottom: 80,
            data: function(currPage) {
                var request = {
                    skey: key,
                    ctype: '',
                    page: currPage,
                    pageSize: pageSize
                }
                request['appid'] = wineApp.ajax.getAppid();
                request['time'] = _.now();
                var sign = wineApp.ajax.signParam(request)
                request['sign'] = sign;
                return JSON.stringify(request);

            },
            path: function(currPage) {
                var offset = currPage * pageSize;
                var ajaxUrl = wineApp.tool.getBaseApiURL() + wineApp.CONFIG.API.winelist;
                return ajaxUrl;
            },
            debug: true
        }, function(data, opts) {
            if (opts.state.currPage <= data.pageCount) {
                onSuccess(data);
            } else {
                stopAjax = true;
            }
        })

    }
    return {
        loadWineList: loadWineList
    }
})();
$(document).ready(function() {
    wineApp.wine.loadWineList();
    wineApp.common.initBanner();
    wineApp.common.initHeaderIcon(0, false);
    wineApp.common.setSelectedMenu('wine');
});